package vip.xiaonuo.pm.modular.enterprise.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.common.param.DeleteByIdParam;
import vip.xiaonuo.pm.modular.enterprise.entity.PmEnterpriseBankEntity;
import vip.xiaonuo.pm.modular.enterprise.mapper.PmEnterpriseBankMapper;
import vip.xiaonuo.pm.modular.enterprise.param.EnterpriseBankAddParam;
import vip.xiaonuo.pm.modular.enterprise.param.EnterpriseBankEditParam;
import vip.xiaonuo.pm.modular.enterprise.param.EnterpriseBankPageParam;
import vip.xiaonuo.pm.modular.enterprise.service.PmEnterpriseBankService;

import java.util.List;

/**
 * <p>
 * 客户银行账户信息 服务实现类
 * </p>
 *
 * @author xhs
 * @since 2025-06-04
 */
@Service
public class PmEnterpriseBankServiceImp extends ServiceImpl<PmEnterpriseBankMapper, PmEnterpriseBankEntity> implements PmEnterpriseBankService {

    /**
     * 根据ID查询数据
     *
     * @param id 入参
     * @return PmEnterpriseEntity
     */
    @Override
    public PmEnterpriseBankEntity queryEntityById(String id) {
        PmEnterpriseBankEntity bankEntity = this.getById(id);
        if (ObjectUtil.isEmpty(bankEntity)) {
            throw new CommonException("客户银行账户信息不存在，id值为：{}", id);
        }
        return bankEntity;
    }

    /**
     * 新增客户银行账户信息
     *
     * @param enterpriseBankAddParam 入参
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(EnterpriseBankAddParam enterpriseBankAddParam) {
        // 1.校验是否存在
        checkParam(enterpriseBankAddParam);
        PmEnterpriseBankEntity entity = BeanUtil.toBean(enterpriseBankAddParam, PmEnterpriseBankEntity.class);
        // 2.新增数据
        this.save(entity);
    }

    private void checkParam(EnterpriseBankAddParam enterpriseBankAddParam) {
        boolean hasEnterpriseBank = this.count(new LambdaQueryWrapper<PmEnterpriseBankEntity>()
                .eq(PmEnterpriseBankEntity::getEnterpriseId, enterpriseBankAddParam.getEnterpriseId())
                .eq(PmEnterpriseBankEntity::getBankName, enterpriseBankAddParam.getBankName())
                .eq(PmEnterpriseBankEntity::getBankAccount, enterpriseBankAddParam.getBankAccount())
        ) > 0;
        if (hasEnterpriseBank) {
            throw new CommonException("存在重复的客户银行账户信息，名称为：{}", enterpriseBankAddParam.getBankName());
        }
    }

    /**
     * 编辑客户银行账户信息
     *
     * @param enterpriseBankEditParam 入参
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(EnterpriseBankEditParam enterpriseBankEditParam) {
        // 1.校验是否存在
        checkParam(enterpriseBankEditParam);
        PmEnterpriseBankEntity enterpriseBankEntity = this.queryEntityById(enterpriseBankEditParam.getId());
        BeanUtil.copyProperties(enterpriseBankEditParam, enterpriseBankEntity);
        // 2.编辑数据
        this.updateById(enterpriseBankEntity);
    }

    private void checkParam(EnterpriseBankEditParam enterpriseBankEditParam) {
        boolean hasEnterpriseBank = this.count(new LambdaQueryWrapper<PmEnterpriseBankEntity>()
                .eq(PmEnterpriseBankEntity::getEnterpriseId, enterpriseBankEditParam.getEnterpriseId())
                .eq(PmEnterpriseBankEntity::getBankName, enterpriseBankEditParam.getBankName())
                .eq(PmEnterpriseBankEntity::getBankAccount, enterpriseBankEditParam.getBankAccount())
                .ne(PmEnterpriseBankEntity::getId, enterpriseBankEditParam.getId())
        ) > 0;
        if (hasEnterpriseBank) {
            throw new CommonException("存在重复的客户银行账户信息，名称为：{}", enterpriseBankEditParam.getBankName());
        }
    }

    /**
     * 删除客户银行账户信息
     *
     * @param DeleteIdParamList 入参
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<DeleteByIdParam> DeleteIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(DeleteIdParamList, DeleteByIdParam::getId));
    }

    /**
     * 获取客户银行账户信息分页
     *
     * @param enterpriseBankPageParam 入参
     * @return Page<PmEnterpriseBankEntity>
     */
    @Override
    public Page<PmEnterpriseBankEntity> page(EnterpriseBankPageParam enterpriseBankPageParam) {
        QueryWrapper<PmEnterpriseBankEntity> queryWrapper = new QueryWrapper<PmEnterpriseBankEntity>().checkSqlInjection();
        queryWrapper.lambda()
                .and((StringUtils.hasLength(enterpriseBankPageParam.getSearchKey())), q ->
                        q.like(StringUtils.hasLength(enterpriseBankPageParam.getSearchKey()), PmEnterpriseBankEntity::getEnterpriseId, enterpriseBankPageParam.getSearchKey())
                                .or()
                                .like(StringUtils.hasLength(enterpriseBankPageParam.getSearchKey()), PmEnterpriseBankEntity::getBankName, enterpriseBankPageParam.getSearchKey())
                )
        ;
        // 处理排序
        if (ObjectUtil.isAllNotEmpty(enterpriseBankPageParam.getSortField(), enterpriseBankPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(enterpriseBankPageParam.getSortOrder());
            queryWrapper.orderBy(true, enterpriseBankPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(enterpriseBankPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(PmEnterpriseBankEntity::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }
}
